package com.project.gjzb.controller;


import com.project.gjzb.dto.BusLineStationDto;
import com.project.gjzb.dto.ParamDto;
import com.project.gjzb.entity.*;
import com.project.gjzb.service.*;
import com.project.gjzb.util.RestResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/gjbz/api/datacenter/")
public class DataCenterController {

    @Resource
    BusLineService busLineService;
    @Resource
    BusLineStationService busLineStationService;
    @Resource
    BusVehicleService busVehicleService;
    @Resource
    BusDirectionService busDirectionService;
    @Resource
    EnterpriseService enterpriseService;

    @PostMapping("busline")
    public RestResult busline(@RequestBody ParamDto paramDto) {
        List<TobjBusLine> tobjBusLineList = busLineService.getList();
        if (tobjBusLineList.size() > 0) {
            busLineService.deleteByDate(paramDto);
            for (TobjBusLine tobjBusLine : tobjBusLineList) {
                tobjBusLine.setRq(paramDto.getDate());
            }
            busLineService.saveList(tobjBusLineList);
            List<BusLine> busLineList = busLineService.getByDate(paramDto.getDate());
            busLineService.deleteAll();
            busLineService.insert(busLineList);
        }
        return RestResult.ok();
    }

    @PostMapping("busline/station")
    public RestResult buslineStation(@RequestBody ParamDto paramDto) {
        List<TobjBusLineStation> tobjBusLineStationList = busLineStationService.getList();
        if (tobjBusLineStationList.size() > 0) {
            busLineStationService.deleteByDate(paramDto);
            for (TobjBusLineStation tobjBusLineStation : tobjBusLineStationList) {
                tobjBusLineStation.setRq(paramDto.getDate());
            }
            busLineStationService.saveList(tobjBusLineStationList);
            List<BusLineStation> busLineStationList = busLineStationService.getByDate(paramDto.getDate());
            busLineStationService.deleteAll();
            busLineStationService.insert(busLineStationList);
        }
        // 重复项删除
        List<BusLineStationDto> repeatList = busLineStationService.getRepeatAll();
        if (repeatList.size() > 0) {
            for (BusLineStationDto busLineStationDto : repeatList) {
                List<BusLineStation> deleteList = busLineStationService.getByRepeat(busLineStationDto);
                if (deleteList.size() > 0) {
                    for (int i = 1; i < deleteList.size(); i++) {
                        busLineStationService.deleteById(deleteList.get(i));
                    }
                }
            }
        }
        // 更新特殊高峰线路时间
        List<TobjBusLineStation> firstList = busLineStationService.getFirst();
        List<TobjBusLineStation> maxList = busLineStationService.getMax();
        if (firstList.size() > 0) {
            for (TobjBusLineStation tobjBusLineStation : firstList) {
                if (maxList.size() > 0) {
                    List<TobjBusLineStation> newList = maxList.stream().filter(item -> item.getBusline_id().equals(tobjBusLineStation.getBusline_id())).collect(Collectors.toList());
                    if (newList.size() > 0) {
                        if (!StringUtils.isEmpty(newList.get(0).getPeakendtime())) {
                            BusLineStation busLineStation1 = new BusLineStation();
                            busLineStation1.setId(tobjBusLineStation.getBusstop_id());
                            busLineStation1.setXlid(tobjBusLineStation.getBusline_id());
                            busLineStation1.setMbsj(newList.get(0).getPeakendtime());
                            busLineStationService.updateSzmbById(busLineStation1);
                        }
                    }
                }
            }
        }
        if (maxList.size() > 0) {
            for (TobjBusLineStation tobjBusLineStation : maxList) {
                BusLineStation busLineStation00 = new BusLineStation();
                busLineStation00.setId(tobjBusLineStation.getBusstop_id());
                busLineStation00.setXlid(tobjBusLineStation.getBusline_id());
                busLineStation00.setSbsj(tobjBusLineStation.getPeakendtime());
                busLineStation00.setZj(String.valueOf(tobjBusLineStation.getStationlevel()));
                busLineStationService.updateMzsbById(busLineStation00);
            }
        }
        return RestResult.ok();
    }

    @PostMapping("busvehicle")
    public RestResult busvehicle(@RequestBody ParamDto paramDto) {
        List<TobjBusVehicle> tobjBusVehicleList = busVehicleService.getList();
        if (tobjBusVehicleList.size() > 0) {
            busVehicleService.deleteByDate(paramDto);
            for (TobjBusVehicle tobjBusVehicle : tobjBusVehicleList) {
                tobjBusVehicle.setRq(paramDto.getDate());
            }
            busVehicleService.saveList(tobjBusVehicleList);
            List<BusVehicle> busVehicleList = busVehicleService.getByDate(paramDto.getDate());
            busVehicleService.deleteAll();
            busVehicleService.insert(busVehicleList);
        }
        return RestResult.ok();
    }

    @PostMapping("busdirection")
    public RestResult busdirection(@RequestBody ParamDto paramDto) {
        List<TobjBusDirection> tobjBusDirectionList = busDirectionService.getList();
        if (tobjBusDirectionList.size() > 0) {
            busDirectionService.deleteByDate(paramDto);
            for (TobjBusDirection tobjBusDirection : tobjBusDirectionList) {
                tobjBusDirection.setRq(paramDto.getDate());
            }
            busDirectionService.saveList(tobjBusDirectionList);
        }
        return RestResult.ok();
    }

    @PostMapping("enterprise")
    public RestResult enterprise(@RequestBody ParamDto paramDto) {
        List<TobjEnterprise> tobjEnterpriseList = enterpriseService.getList();
        if (tobjEnterpriseList.size() > 0) {
            enterpriseService.deleteByDate(paramDto);
            for (TobjEnterprise tobjEnterprise : tobjEnterpriseList) {
                tobjEnterprise.setRq(paramDto.getDate());
            }
            enterpriseService.saveList(tobjEnterpriseList);
            List<Enterprise> enterpriseList = enterpriseService.getByDate(paramDto.getDate());
            enterpriseService.deleteAll();
            enterpriseService.insert(enterpriseList);
        }
        return RestResult.ok();
    }
}
